package com.xc.ksxt.service;

import com.google.common.collect.Lists;
import com.xc.ksxt.entity.LibChoiceEntity;
import com.xc.ksxt.vo.LibChoiceVO;
import com.xc.common.object.PageInfo;
import com.xc.db.Tables;
import com.xc.db.tables.LibChoice;
import com.xc.db.tables.records.LibChoiceRecord;
import java.util.List;

import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.InsertResultStep;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SelectForUpdateStep;
import org.jooq.SelectSeekStepN;
import org.jooq.SortField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LibChoiceService {
	@Autowired
	private DSLContext dsl;
	private LibChoice LIBCHOICETB = LibChoice.LIB_CHOICE.as("LIBCHOICETB");
	public LibChoice getTable() {
		return LIBCHOICETB;
	}
	public Result<Record> select(Condition whr, PageInfo<LibChoiceEntity> pageInfo, SortField<?>... orderby) {
		SelectSeekStepN<Record> seek = dsl.select().from(LIBCHOICETB).where(whr).orderBy(orderby);
		Result<Record> result = null;
		if (pageInfo != null) {
			SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
			result = su.fetch();
		} else {
			result = seek.fetch();
		}
		if (pageInfo != null) {
			pageInfo.setResult(result);
			pageInfo.setList(result.into(LibChoiceEntity.class));
		}
		return result;
	}
	public Result<Record> selectVO(Condition whr, PageInfo<LibChoiceVO> pageInfo, SortField<?>... orderby) {
		SelectSeekStepN<Record> seek = dsl.select().from(LIBCHOICETB).where(whr).orderBy(orderby);
		Result<Record> result = null;
		if (pageInfo != null) {
			SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
			result = su.fetch();
		} else {
			result = seek.fetch();
		}
		if (pageInfo != null) {
			pageInfo.setResult(result);
			pageInfo.setList(result.into(LibChoiceVO.class));
		}
		return result;
	}

	public List<LibChoiceEntity> select(Condition whr, SortField<?>... orderby) {
		Result<Record> result = dsl.select().from(LIBCHOICETB).where(whr).orderBy(orderby).fetch();
		List<LibChoiceEntity> list = Lists.newArrayList();
		if (result.size() > 0) {
			list = result.into(LibChoiceEntity.class);
		}
		return list;
	}

	public Long count(Condition cond) {
		Long count = dsl.selectCount().from(LIBCHOICETB).where(cond).fetchOneInto(Long.class);
		return count;
	}

	public Record getRecordById(Integer id) {
		Record record = dsl.select().from(LIBCHOICETB).where(LIBCHOICETB.ID.eq(id)).fetchOne();
		return record;
	}

	public LibChoiceEntity getEntityById(Integer id) {
		Record r = getRecordById(id);
		if(r==null) {
			return null;
		}
		LibChoiceEntity entity = r.into(LibChoiceEntity.class);
		return entity;
	}

	public Integer update(LibChoiceEntity entity) {
		LibChoiceRecord record = new LibChoiceRecord();
		record.from(entity);
		dsl.executeUpdate(record);
		return Integer.valueOf(entity.getId());
	}

	public Integer insert(LibChoiceEntity entity) {
		LibChoiceRecord record = new LibChoiceRecord();
		record.from(entity);
		InsertResultStep<LibChoiceRecord> su = dsl.insertInto(Tables.LIB_CHOICE).set(record).returning(getTable().ID);
		int id = su.fetchOne().getId();
		entity.setId(id);
		return id;
	}

	public Integer save(LibChoiceEntity entity) {
		if (entity.getId()==null) {
			return insert(entity);
		}
		return update(entity);
	}

	public void deleteById(Integer id) {
		dsl.delete(LIBCHOICETB).where(LIBCHOICETB.ID.eq(id)).execute();
	}
}
